﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Aprendete.Models;

namespace Aprendete.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {


         public IFormsAuthenticationService FormsService { get; set; }
        public IMembershipService MembershipService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }

        public ActionResult Index()
        {            
            return View();
        }

        [ActionName("desloggear")]
        public JsonResult desloggear()
        {
            if (Request.IsAuthenticated)
                FormsService.SignOut();
            return new JsonResult();
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Registrar()
        {
            return View();
        }

        public ActionResult Contacto()
        {
            return View();
        }

  

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Registrar(string nick, string password, string nombre, string apellido, string mail, string rut, string colegio, string telefono, string curso, string nacimiento)
        {
            try
            {
                nacimiento = nacimiento.Substring(3, 3) + nacimiento.Substring(0, 3) + nacimiento.Substring(6);

                Usuario us = new Usuario();
                us.Nombre = nombre;
                us.Apellido = apellido;
                us.UserName = nick;
                us.Rut = rut;
                us.Password = password;
                us.Mail = mail;
                us.Colegio = colegio;
                us.Telefono = telefono;
                us.Curso = curso;
                us.Nacimiento = System.DateTime.Now;
                UsuarioAdmin ua = new UsuarioAdmin();
                bool registrado = ua.RegistrarUsuario(us);
                if (registrado)
                    return RedirectToAction("satisfactorio");
                else
                    return RedirectToAction("fallido");
            }
            catch (Exception e)
            {
                return RedirectToAction("fallido");
            }

        }

        public ActionResult fallido()
        {
            return View();
        }
        public ActionResult satisfactorio()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Logonx(string username, string password)
        {
            UsuarioAdmin ua = new UsuarioAdmin();
            Usuario uss = ua.Login(username, password);
            if (uss == null)
            {
                return RedirectToAction("Index", "Home");
            }
            else
            {
                FormsService.SignIn(username, true);
                return RedirectToAction("Index", "Usuario");
            }
            
        }









    }

}
